*' --------------
* sample characteristics ----
*' ==============

clear all
* set up your working directory here
global home_dir ""

cd ${home_dir}


cap: program drop table_descriptive
program table_descriptive 

	args geo_type vv 

	use "./data/processed_notshared/cleaned_regtable_unemployment_`geo_type'_v2.dta", clear 
	
	svyset [pw=weight] 	
	
	if ("`vv'" == "v1"){
		keep if sample_v1 == 1	
	}
	if ("`vv'" == "v2") {
		keep if sample_v2 == 1	
	}
	
	
	local demographics_individual Female Age White_nh Black_nh AIAN_nh AsPI_nh Other_nh Hispanic BornUSA 
	local demographics_county RAT_Female RAT_AgeGrp4_2 RAT_AgeGrp4_3 RAT_AgeGrp4_4 RAT_Race6_2 RAT_Race6_3 RAT_Race6_4 RAT_Race6_5 RAT_Race6_6 RAT_BornUSA 
	local contextual_control pop_density
	local post_control RAT_EducGroup_1 RAT_EducGroup_2 RAT_EducGroup_3 RAT_EducGroup_4 RAT_EducGroup_5
	
	egen n_miss = rowmiss(Suic St `geo_type' Year `demographics_individual' `demographics_county' `contextual_control')
	recode n_miss (0=0) (1/max=1)
	
	* check how many missings
	*tab n_miss Suic, col
	drop if n_miss > 0
	
	eststo clear 
	eststo all_group: estpost summarize `demographics_individual' `demographics_county' `contextual_control' `post_control' [aw=weight] if sample_v2 == 1 
	eststo emp_group: estpost summarize `demographics_individual' `demographics_county' `contextual_control' `post_control' [aw=weight] if sample_v2 == 1 & empstat == 1
	eststo unemp_group: estpost summarize `demographics_individual' `demographics_county' `contextual_control' `post_control' [aw=weight] if sample_v2 == 1 & empstat == 2
	eststo nil_group: estpost summarize `demographics_individual' `demographics_county' `contextual_control' `post_control' [aw=weight] if sample_v2 == 1 & empstat == 3
	
	esttab * using "./output/`vv'_table1_comparison_`geo_type'.csv", csv replace label cells("mean(pattern(1 1 1 1) fmt(a2)) sd(pattern(1 1 1 1) fmt(a2)) count(pattern(1 1 1 1) fmt(a2))") 

end 

table_descriptive county "v2"
table_descriptive cz "v2"

* check other descriptive stats
local geo_type county
local vv "v2"

use "./data/processed_notshared/cleaned_regtable_unemployment_`geo_type'_v2.dta", clear 

svyset [pw=weight] 	

if ("`vv'" == "v1"){
	keep if sample_v1 == 1	
}
if ("`vv'" == "v2") {
	keep if sample_v2 == 1	
}


local demographics_individual Female age Race6 BornUSA 
local demographics_county RAT_Female RAT_AgeGrp4_2 RAT_AgeGrp4_3 RAT_AgeGrp4_4 RAT_Race6_2 RAT_Race6_3 RAT_Race6_4 RAT_Race6_5 RAT_Race6_6 RAT_BornUSA 
local contextual_control pop_density
local post_control RAT_EducGroup_1 RAT_EducGroup_2 RAT_EducGroup_3 RAT_EducGroup_4 RAT_EducGroup_5

egen n_miss = rowmiss(Suic St `geo_type' Year `demographics_individual' `demographics_county' `contextual_control')
recode n_miss (0=0) (1/max=1)

* for appendix table : compare emp/nonemp/notinlabor + overall
*sum Suic St `geo_type' Year `demographics_individual' `demographics_county' `contextual_control'

drop if n_miss > 0
*(1,291 observations deleted)

* for descriptive figure 1  
mean Suic [pw=weight], over(empstat)
matrix B = r(table)
mat2txt, matrix(B) saving("./output/mean_suicide_rate_overall_all.txt") replace

mean Suic [pw=weight], over(Year)
matrix B = r(table)
mat2txt, matrix(B) saving("./output/mean_suicide_rate_year_all.txt") replace

forvalues i = 1/3 {
	mean Suic [pw=weight] if empstat == `i', over(Year)
	mat2txt, matrix(B) saving("./output/mean_suicide_rate_year_empstat`i'.txt") replace

}
